﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication.ExtendedProtection;
using System.Threading.Tasks;
using DotNetStar.TheKing.Domain;
using DotNetStar.TheKing.Infrastructure;
using DotNetStar.TheKing.Models;
using DotNetStar.TheKing.Services;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DotNetStar.TheKing.Web.Pages.Content
{
    public class IndexModel : PageModel
    {
        private readonly ContentService _contentService;
        private readonly IHostingEnvironment _environment;
        public IndexModel(ContentService contentService, IHostingEnvironment environment)
        {
            _contentService = contentService;
            _environment = environment;
        }

        public PagedCollection<Domain.Content> Contents { get; set; }

        ///// <summary>
        ///// 热门帖子
        ///// </summary>
        //public IEnumerable<Domain.Content> HotContent { get; set; }
        ///// <summary>
        ///// 当前分类相关排行
        ///// </summary>
        //public IEnumerable<Domain.Content> HotRelatedContent { get; set; }

        public Category CategoryInfo { get; set; }

        public async Task<IActionResult> OnGetAsync(int? categoryId,Channels? channel)
        {

            this.Contents = await _contentService.GetContentsAsync(new ContentPageQuery(Request.Query["page"].ToString().To(1))
            {
                CategoryId = categoryId,
                Channel = channel
            });

            if (categoryId.HasValue)
            {
                this.CategoryInfo = await _contentService.GetCategoryAsync(categoryId.Value);
            }
            //this.HotContent = _contentService.GetTopContentQueable(ContentQuery.Default.OnlyContentChannel())
            //    .OrderByDescending(m => m.VisitNum)
            //    .ToList();

            //this.HotRelatedContent = _contentService.GetTopContentQueable(new ContentQuery
            //{
            //     CategoryId=categoryId
            //})
            //    .OrderByDescending(m => m.VisitNum)
            //    .ToList();

            //return Page();

            return RedirectToPage($"Index{channel}", new { categoryId, channel });
        }
    }
}